<?php
class ContentTypesModel {
	public static function getByID ($id) {
		return self::get(array(
			'limit' => 1, 
			'where' => 'content_types_id = ' . SleekPHP::db()->escSQL($id)
		));
	}

	public static function get ($config) {
		return SleekPHP::db()->get('content_types', $config);
	}

	public static function insert ($row) {
		return self::insertUpdate($row);
	}

	public static function update ($id, $row) {
		return self::insertUpdate($row, $id);
	}

	private static function insertUpdate ($row, $id = false) {
		$title	= isset($row['title']) ? $row['title'] : 'Untitled';
		$slug	= isset($row['slug']) ? $row['slug'] : urlize($title);

		$fields = array(
			'title'	=> $title, 
			'slug'	=> $slug
		);

		if ($id) {
			return SleekPHP::db()->update('content_types', $id, $fields);
		}
		else {
			return SleekPHP::db()->insert('content_types', $fields);
		}
	}

	public static function delete ($id) {
		return SleekPHP::db()->delete('content_types', $id);
	}
}
